package com.xm.httpapi.Aspect;

import com.xm.httpapi.BaseUtils.LogUtils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
/**
 * Author:guohaijin
 * Date:
 * Description:
 */
@Aspect
public class TimeLogAspect {
    @Pointcut("execution(@com.xm.httpapi.Annotation.TimeLog * *(..))")//方法切入点
    public void methodAnnotated() {
    }

    @Pointcut("execution(@com.xm.httpapi.Annotation.TimeLog *.new(..))")//构造器切入点
    public void constructorAnnotated() {
    }

    @Around("methodAnnotated() || constructorAnnotated()")//在连接点进行方法替换
    public Object aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getName();
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();//执行原方法
        LogUtils.e((className + "." + methodName + " --->:" + "[" + (System.currentTimeMillis() - startTime) + "毫秒]"));// 打印时间差
        return result;
    }
}
